﻿

using Dapper;
using hdal;
using hdb;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
 namespace hwms {
    public abstract class WcsBaseEngine :GkTransaction {
        private static object obj = new object();
        private IDbConnection _connection;
        override protected IDbConnection getConnection() {
            if( _connection == null ) {
                _connection = DBUtils.newWCSConn(BaseConfig.prefixal);
            }
            return _connection;
        }
        public int BBTaskNo(IDbConnection conn ,IDbTransaction trans) {
            lock( obj ) {
                int value = 1;
                DateTime dt = DateTime.Now;
                String dateline = dt.ToString("yyyyMMdd");
                int now = int.Parse(dateline);
                string sql1 = "SELECT v FROM b_Task_No WHERE k= @now";
              
                List<int> tc = conn.Query<int>(sql1 ,new { now  } ,trans).AsList();
                if( tc.Count == 0 ) {
                    conn.Execute("insert into b_Task_No (k,v) values (@now,@value)" ,new { now ,value } ,trans);
                } else {
                    value = tc[0] + 1;
                    string sql2 = "UPDATE b_Task_No SET v=@v WHERE k=@now";
                    conn.Execute(sql2 ,new { v = value ,now } ,trans);
                }
                string begin = now%10000+"";
                if( value < 10 ) {
                    begin += "00" + value;
                } else if( value < 100 ) {
                    begin += "0" + value;
                } else {
                    begin += value;
                }
           
             
                return int.Parse(begin);
            }
            
        }
        
      
       
      
   
      
      
         
      
    }
}
